package com.xiaomo.gitee.datamanager.controller.admin;

import com.xiaomo.gitee.datamanager.common.result.Code;
import com.xiaomo.gitee.datamanager.common.result.Result;
import com.xiaomo.gitee.datamanager.dto.tag.CategoryTagDTO;
import com.xiaomo.gitee.datamanager.dto.tag.ImageTagDTO;
import com.xiaomo.gitee.datamanager.service.tag.CategoryTagService;
import com.xiaomo.gitee.datamanager.service.tag.ImageTagService;
import com.xiaomo.gitee.datamanager.service.tag.TagsService;
import com.xiaomo.gitee.datamanager.service.tag.impl.CategoryTagServiceImpl;
import com.xiaomo.gitee.datamanager.service.tag.impl.ImageTagServiceImpl;
import com.xiaomo.gitee.datamanager.service.tag.impl.TagsServiceImpl;
import com.xiaomo.gitee.datamanager.util.JwtUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/api")
@Tag(name = "标签管理", description = "管理员操作图片标签和数据集标签")
@RequiredArgsConstructor
public class AdminTagController {

    private final ImageTagService imageTagService;
    private final TagsService tagsService;
    private final CategoryTagService categoryTagService;

    @Autowired
    public AdminTagController(ImageTagServiceImpl imageTagService, TagsServiceImpl tagsService, CategoryTagServiceImpl categoryTagService) {
        this.imageTagService = imageTagService;
        this.tagsService = tagsService;
        this.categoryTagService = categoryTagService;
    }

    @PutMapping("/tag")
    @PreAuthorize("hasAuthority('tag_cd')")
    @Operation(
            summary = "创建新标签",
            security = @SecurityRequirement(name = JwtUtils.TOKEN_HEADER),
            parameters = {
                    @Parameter(
                            name = "name",
                            description = "标签显示名称",
                            required = true
                    )
            },
            responses = {
                    @ApiResponse(
                            responseCode = Code.SUCCESS,
                            description = "操作成功",
                            content = @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(
                                            implementation = Result.class
                                    )
                            )
                    ),
                    @ApiResponse(
                            responseCode = "40902",
                            description = "标签名已存在",
                            content = @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(
                                            implementation = Result.class
                                    )
                            )
                    )
            }
    )
    public Result<?> createTag(@RequestParam String name) {
        int result = tagsService.createTag(name);
        return switch (result) {
            case 2 -> Result.fail(40902,"标签名已存在");
            default -> Result.ok();
        };
    }



    @PostMapping("/image-tag")
    @PreAuthorize("hasAuthority('tag_stick')")
    @Operation(
            summary = "为图片添加标签",
            security = @SecurityRequirement(name = JwtUtils.TOKEN_HEADER),
            parameters = {
                    @Parameter(
                            name = "data",
                            description = "图片标签获取模型",
                            required = true,
                            schema = @Schema(
                                    implementation = ImageTagDTO.class
                            )
                    )
            },
            responses = {
                    @ApiResponse(
                            responseCode = Code.SUCCESS,
                            description = "操作成功",
                            content = @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(
                                            implementation = Result.class
                                    )
                            )
                    ),
                    @ApiResponse(
                            responseCode = "409",
                            description = "该图片已存在此标签",
                            content = @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(
                                            implementation = Result.class
                                    )
                            )
                    )
            }
    )
    public Result<?> addTagToImage(@RequestBody ImageTagDTO data) {
        if (!imageTagService.addTagToImage(data.getImageId(),data.getTagsId())) {
            return Result.fail(409,"该图片已存在此标签");
        }
        return Result.ok();
    }

    @DeleteMapping("/image-tag")
    @PreAuthorize("hasAuthority('tag_stick')")
    @Operation(
            summary = "删除图片标签",
            security = @SecurityRequirement(name = JwtUtils.TOKEN_HEADER),
            parameters = {
                    @Parameter(
                            name = "data",
                            description = "图片标签传输模型",
                            required = true,
                            schema = @Schema(
                                    implementation = ImageTagDTO.class
                            )
                    )
            },
            responses = {
                    @ApiResponse(
                            responseCode = Code.SUCCESS,
                            description = "操作成功",
                            content = @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(
                                            implementation = Result.class
                                    )
                            )
                    )
            }
    )
    public Result<?> deleteTagOfImage(@RequestBody  ImageTagDTO data) {
        imageTagService.deleteTagOfImage(data.getImageId(), data.getTagsId());
        return Result.ok();
    }

    @PostMapping("/category-tag")
    @PreAuthorize("hasAuthority('tag_stick')")
    @Operation(
            summary = "为数据集添加标签",
            security = @SecurityRequirement(name = JwtUtils.TOKEN_HEADER),
            parameters = {
                    @Parameter(
                            name = "data",
                            description = "数据集标签传输模型",
                            required = true,
                            schema = @Schema(
                                    implementation = CategoryTagDTO.class
                            )
                    )
            },
            responses = {
                    @ApiResponse(
                            responseCode = Code.SUCCESS,
                            description = "操作成功",
                            content = @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(
                                            implementation = Result.class
                                    )
                            )
                    ),
                    @ApiResponse(
                            responseCode = "409",
                            description = "该数据集已包含此标签",
                            content = @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(
                                            implementation = Result.class
                                    )
                            )
                    )
            }
    )
    public Result<?> addTagToSet(@RequestBody CategoryTagDTO data) {
        if (!categoryTagService.addTagToSet(data.getCategoryId(), data.getTagsId())) {
            return Result.fail(409,"该数据集已包含此标签");
        }
        return Result.ok();
    }

    @PostMapping("/tag")
    @PreAuthorize("hasAuthority('tag_update')")
    @Operation(
            summary = "修改标签",
            description = "修改已存在的标签的名称",
            security = @SecurityRequirement(name = JwtUtils.TOKEN_HEADER),
            parameters = {
                    @Parameter(
                            name = "id",
                            description = "标签ID",
                            required = true
                    ),
                    @Parameter(
                            name = "newName",
                            description = "标签新名称",
                            required = true
                    )
            },
            responses = {
                    @ApiResponse(
                            responseCode = Code.SUCCESS,
                            description = "操作成功",
                            content = @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(
                                            implementation = Result.class
                                    )
                            )
                    ),
                    @ApiResponse(
                            responseCode = "40901",
                            description = "已存在相同标签",
                            content = @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(
                                            implementation = Result.class
                                    )
                            )
                    ),
                    @ApiResponse(
                            responseCode = "40902",
                            description = "新名称与原来相同",
                            content = @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(
                                            implementation = Result.class
                                    )
                            )
                    )
            }
    )
    public Result<?> updateTag(@RequestParam Long id, @RequestParam String newName) {
        int result = tagsService.updateTag(newName, id);
        return switch (result) {
            case 1 -> Result.fail(40901,"已存在相同标签");
            case 2 -> Result.fail(40902,"新名称与原来相同");
            default -> Result.ok();
        };
    }


    @DeleteMapping("/tag")
    @PreAuthorize("hasAuthority('tag_cd')")
    @Operation(
            summary = "删除标签",
            description = "删除一个已存在的标签",
            security = @SecurityRequirement(name = JwtUtils.TOKEN_HEADER),
            parameters = {
                    @Parameter(
                            name = "idList",
                            description = "标签ID列表",
                            required = true
                    )
            },
            responses = {
                    @ApiResponse(
                            responseCode = Code.SUCCESS,
                            description = "操作成功",
                            content = @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(
                                            implementation = Result.class
                                    )
                            )
                    )
            }
    )
    public Result<?> deleteTag(@RequestBody List<Long> idList) {
        tagsService.deleteTag(idList);
        return Result.ok();
    }

    @DeleteMapping("/category-tag")
    @PreAuthorize("hasAuthority('tag_cd')")
    @Operation(
            summary = "删除数据集标签",
            description = "删除数据集标签，并删除该数据集下所有图片的对应标签(通过给数据集添加标签而得到的标签)",
            security = @SecurityRequirement(name = JwtUtils.TOKEN_HEADER),
            parameters = {
                    @Parameter(
                            name = "data",
                            description = "图片标签传输模型",
                            required = true,
                            schema = @Schema(
                                    implementation = Long.class
                            )
                    )
            },
            responses = {
                    @ApiResponse(
                            responseCode = Code.SUCCESS,
                            description = "操作成功",
                            content = @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(
                                            implementation = Result.class
                                    )
                            )
                    )
            }
    )
    public Result<?> deleteTagOfSet(@RequestBody CategoryTagDTO data) {
        categoryTagService.deleteTagOfSet(data.getCategoryId(),data.getTagsId());
        return Result.ok();
    }

}
